//
//  HZL_FindSceneTableViewCell.m
//  B_Project
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_FindSceneTableViewCell.h"
#import "HZL_FindSceneCollectionViewCell.h"
#import "HZL_SceneViewController.h"
#import "HZL_SecondMainViewController.h"

@interface HZL_FindSceneTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *sceneDataArr;

@end

@implementation HZL_FindSceneTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.sceneDataArr = [NSMutableArray array];
        [self getData];
        [self createCollection];
    }
    return self;
}
#pragma mark - 获取数据
- (void)getData
{
    // 场景
    [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/scene/getSceneListV1_7" body:nil completion:^(id result) {
        if (result) {
            NSMutableArray *temp = result[@"body"][@"list"];
            self.sceneDataArr = [HZL_FindModel transformWithArray:temp];
  
            [self.collectionView reloadData];
        }
    }];
}
- (void)createCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(140, 110);
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 10);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    // 竖滑
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 110) collectionViewLayout:layout];
    [self.contentView addSubview:self.collectionView];
    // 隐藏滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[HZL_FindSceneCollectionViewCell class] forCellWithReuseIdentifier:@"item1"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.sceneDataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_FindSceneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item1" forIndexPath:indexPath];
    cell.model = self.sceneDataArr[indexPath.item];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    HZL_SceneViewController *scene = [[HZL_SceneViewController alloc] init];
    scene.Id = [self.sceneDataArr[indexPath.item] iD];
    scene.hidesBottomBarWhenPushed = YES;
    scene.typeModelArr = self.sceneDataArr;
    UIResponder *responder = self.nextResponder.nextResponder.nextResponder.nextResponder;
    [((UIViewController *)responder).navigationController pushViewController:scene animated:YES];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
